package com.study.mongoDB;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

import com.google.gson.Gson;

/**
 * Hello world!
 *
 */
public class MongoDB 
{
	private static final String _uuid="uuid";
	
	/***
	 *创建数据库
	 * @param path
	 * @throws Exception
	 */
	public void createDataBase(String path) throws Exception{
		
		FileOutputStream fileOutStream=new FileOutputStream(path);
		
		Document document=new Document();
		
		Element root=new Element("database");
		
		Element table1=new Element("table");
		table1.setAttribute("name","orderLog");
		
		Element table2=new Element("table");
		table2.setAttribute("name", "custLog");
		
		Element table3=new Element("table");
		table3.setAttribute("name","pointLog");
		
		root.addContent(table1);
		root.addContent(table2);
		root.addContent(table3);
		
		document.setRootElement(root);
		
		XMLOutputter out=new XMLOutputter();
		out.output(document, fileOutStream);
		
		System.out.println("create database success!");		
	}
	/***
	 * 获取某个表中的记录
	 * @param path
	 * @param tablename
	 * @return
	 * @throws Exception
	 */
	public List<Map> loadTableDatas(String path,String tablename) throws Exception{
		
		Gson gson=new Gson();
		
		FileInputStream fileInput=new FileInputStream(path);		
		SAXBuilder saxBuilder=new SAXBuilder();		
		Document docment=saxBuilder.build(fileInput);		
		Element root=docment.getRootElement();
		
		List<Element> taleList=root.getChildren();		
		List<Map> dataMap=new ArrayList<Map>();
		
		for(Element table:taleList){			
			if(table.getAttributeValue("name").equals(tablename)){				
				List<Element> rowList=table.getChildren();				
				for(Element row:rowList){					
					String data=row.getText();
					if(data==null){
						break;
					}
					Map mp=gson.fromJson(data, Map.class);					
					dataMap.add(mp);					
				}				
			}			
		}		
		return dataMap;
	}
	/***
	 * 插入数据
	 * @param path
	 * @param tablename
	 * @param po
	 * @return
	 * @throws Exception
	 */
	public String addData(String path,String tablename,Object po) throws Exception{
		
		Gson gson=new Gson();
		FileInputStream file=new FileInputStream(path);
		String uuid=null;
		SAXBuilder saxBuilder=new SAXBuilder();
		Document document=saxBuilder.build(file);
		
		List<Element> tableList=document.getRootElement().getChildren();
		for(Element table:tableList){
			if(table.getAttributeValue("name").equals(tablename)){
				uuid=UUID.randomUUID().toString();
				String json=gson.toJson(po);
				Map inputMap= gson.fromJson(json,Map.class);		
				
				Map insertMap=new HashMap();
				
				insertMap.putAll(inputMap);
				insertMap.put(_uuid,uuid);
				Element data=new Element("data");
				data.addContent(gson.toJson(insertMap));				
				table.addContent(data);				
			}
		}
		XMLOutputter out=new XMLOutputter();
		
		out.output(document,new FileOutputStream(path));
		
		System.out.println("插入数据成功");
		
		return uuid;
	} 

	/***
	 * 修改数据
	 * @param path
	 * @param tablename
	 * @param po
	 * @return
	 * @throws Exception
	 */
	public boolean updateData(String path,String tablename,Object po) throws Exception{
		
		Gson gson=new Gson();
		FileInputStream file=new FileInputStream(path);
		
		SAXBuilder saxBuilder=new SAXBuilder();
		Document document=saxBuilder.build(file);
		
		List<Element> tableList=document.getRootElement().getChildren();
		for(Element table:tableList){
			if(table.getAttributeValue("name").equals(tablename)){
				
				String json=gson.toJson(po);
				Map newMap= gson.fromJson(json,Map.class);	
				List<Element> rows=table.getChildren();
				for(Element row:rows){
					
					Map oldRow=gson.fromJson(row.getText(),Map.class);					
					if(oldRow.get(_uuid).equals(newMap.get(_uuid))){
						oldRow.putAll(newMap);
						
						Element data=new Element("data");
						data.setText(gson.toJson(oldRow));
						
						table.removeContent(row);
						table.addContent(data);
						break;						
					}
				}
			}
		}
		XMLOutputter out=new XMLOutputter();
		
		out.output(document,new FileOutputStream(path));
		
		System.out.println("更新数据成功");
		
		return true;
	}
	
	/***
	 * 删除数据
	 * @param path
	 * @param tablename
	 * @param po
	 * @return
	 * @throws Exception
	 */
	public boolean deleteData(String path,String tablename,Object po) throws Exception{
		
		Gson gson=new Gson();
		FileInputStream file=new FileInputStream(path);
		
		SAXBuilder saxBuilder=new SAXBuilder();
		Document document=saxBuilder.build(file);
		
		List<Element> tableList=document.getRootElement().getChildren();
		for(Element table:tableList){
			if(table.getAttributeValue("name").equals(tablename)){
				
				String json=gson.toJson(po);
				Map newMap= gson.fromJson(json,Map.class);	
				List<Element> rows=table.getChildren();
				for(Element row:rows){
					
					Map oldRow=gson.fromJson(row.getText(),Map.class);					
					if(oldRow.get(_uuid).equals(newMap.get(_uuid))){					 
						table.removeContent(row); 
						break;						
					}
				}
			}
		}
		XMLOutputter out=new XMLOutputter();
		
		out.output(document,new FileOutputStream(path));
		
		System.out.println("删除数据成功");
		
		return true;
	}
}
